//
//  OPNoteDetailViewController.m
//  OnePiece
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 wsy. All rights reserved.
//

#import "OPNoteDetailViewController.h"
#import "OPNoteDetailHeaderView.h"
#import "OPNoteDetailCell.h"
#import "OPNoteImageViewController.h"
#import "OPNoteMapViewController.h"

@interface OPNoteDetailViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) OPNoteDetailHeaderView *headerView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, strong) OPNoteDetailHeaderModel *dataSourceModel;
@property (nonatomic, strong) NSArray *records;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) CGFloat realH;
@property (nonatomic, assign) CGFloat realW;
@property (nonatomic, strong) UIImageView *coverImg;
@property (nonatomic, strong) UIButton *pathItem;
@property (nonatomic, strong) NSArray *pathMapArray;
@property (nonatomic, strong) UIButton *backBtn;

@end

@implementation OPNoteDetailViewController


- (NSMutableArray *)dataSourceArray
{
    if (_dataSourceArray == nil) {
        self.dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",self.noteId);
    [self configureData];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[OPNoteDetailCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];

    self.realH = SCREEN_HEIGHT / 2 - 40;
    self.realW = (SCREEN_HEIGHT / 2 - 40) * 640 / 436;
    _coverImg = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - _realW / 2, 0, _realW, _realH)];
    [self.view addSubview:_coverImg];

    _pathItem = [[UIButton alloc]init];
    _pathItem.frame = CGRectMake(SCREEN_WIDTH - 80, 80, 100, 25);
    _pathItem.layer.cornerRadius = 12.5;
    _pathItem.layer.masksToBounds = YES;
    _pathItem.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    _pathItem.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.509];
    [_pathItem setTitle:@"行程图" forState:(UIControlStateNormal)];
    _pathItem.tintColor = [UIColor whiteColor];
    _pathItem.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
    [_pathItem setImage:[UIImage imageNamed:@"zyciconfont-didian"] forState:(UIControlStateNormal)];
    [_pathItem addTarget:self action:@selector(didClickMapVC) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_pathItem];
    [self.view bringSubviewToFront:_pathItem];
    
    _backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _backBtn.frame = CGRectMake(20, 40, 40, 30);
    _backBtn.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.502];
    _backBtn.layer.cornerRadius = 5;
    [_backBtn setImage:[UIImage imageNamed:@"zyciconfont-houtui"] forState:(UIControlStateNormal)];
    [_backBtn addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_backBtn];
    
    
}

- (void)didClickMapVC
{
    OPNoteMapViewController *mapVC = [[OPNoteMapViewController alloc]init];
    mapVC.pathMapArray = self.pathMapArray;
    [self.navigationController pushViewController:mapVC animated:YES];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addHeader
{
    _headerView = [[OPNoteDetailHeaderView alloc]init];
    self.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 2 + [self.headerView heightForString:_dataSourceModel.foreword]);
    [self.headerView configuredataWithModel:self.dataSourceModel];
    self.tableView.tableHeaderView = _headerView;
    [self.coverImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.117go.com/timg/upload/%@",_dataSourceModel.coverpic]]placeholderImage:[UIImage imageNamed:@"beijingImg.jpg"]];
    
    
    [self.view insertSubview:_coverImg belowSubview:_tableView];
}

- (void)configureData
{
    [OPRequesterManager requestWithType:GET urlString:[NSString stringWithFormat:@"http://app6.117go.com/demo27/php/formAction.php?recType=0&refer=PlazaHome&id1=0&id2=2&submit=getATour2&tourid=%@&v=i6.4.2&vc=AppStore&vd=ba172e49ddb4ea7a&lang=zh-Hans&verify=e8b8f1665bdcc87377852447c4716752",self.noteId] parDic:nil finish:^(NSData *data) {
        NSDictionary *d = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        _dataSourceModel = [[OPNoteDetailHeaderModel alloc]init];
        [_dataSourceModel setValuesForKeysWithDictionary:d[@"obj"]];
        self.records = d[@"obj"][@"records"];
        self.pathMapArray = d[@"obj"][@"PathMap"];
        NSLog(@"%ld",((NSArray *)d[@"obj"][@"PathMap"]).count);
        for (NSDictionary *dd in d[@"obj"][@"records"]) {
            OPNoteDetailModel *m = [[OPNoteDetailModel alloc]init];
            [m setValuesForKeysWithDictionary:dd];
            [self.dataSourceArray addObject:m];
        }
        [self.tableView reloadData];
        [self addHeader];
    } error:^(NSError *error) {
        NSLog(@"错误");
    }];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OPNoteDetailModel *m = self.dataSourceArray[indexPath.row];
    if ([OPNoteDetailCell heightForModel:m] > 0) {
        return [OPNoteDetailCell heightForModel:m];
    }
    else
    {
        return 0;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OPNoteDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    OPNoteDetailModel *m = self.dataSourceArray[indexPath.row];
    [cell configureDataWithModel:m];
    cell.backgroundColor = [UIColor colorWithRed:1.000 green:0.845 blue:0.750 alpha:1.000];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OPNoteDetailModel *m = _dataSourceArray[indexPath.row];
    if (m.video_file_640.length > 0) {
        return;
    }
    OPNoteImageViewController *imageVC = [[OPNoteImageViewController alloc]init];
    imageVC.records = self.records;
    imageVC.num = indexPath.row;
    imageVC.view.frame = CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT - 20);
    imageVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:imageVC animated:YES completion:nil];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    if (scrollView.contentOffset.y < 0) {
        CGFloat num = (_realH - offsetY) / _realH;
        CGFloat imgH = _realH * num;
        CGFloat imgW = _realW * num;
        _coverImg.frame = CGRectMake(SCREEN_WIDTH / 2 - imgW / 2, 0, imgW, imgH);
    }
    if (scrollView.contentOffset.y > 0) {
        CGFloat imgH = SCREEN_HEIGHT / 2 - 40;
        CGFloat imgW = (SCREEN_HEIGHT / 2 - 40) * 640 / 436;
        _coverImg.frame = CGRectMake(SCREEN_WIDTH / 2 - imgW / 2, 0 - offsetY, imgW, imgH);
    }
    
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
